package tv.huan.tencent.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tv.huan.common.core.text.Convert;
import tv.huan.common.utils.DateUtils;
import tv.huan.media.rabbit.RabbitConstant;
import tv.huan.system.mapper.SysDictDataMapper;
import tv.huan.tencent.mapper.CardPoolMapper;
import tv.huan.tencent.domain.CardPool;
import tv.huan.tencent.service.ICardPoolService;

/**
 * 卡池Service业务层处理
 * 
 * @author admin
 * @date 2020-12-23
 */
@Service
public class CardPoolServiceImpl implements ICardPoolService 
{
    @Autowired
    private CardPoolMapper cardPoolMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Autowired
    private AmqpTemplate rabbitmqTemplate;

    /**
     * 查询卡池
     * 
     * @param cardId 卡池ID
     * @return 卡池
     */
    @Override
    public CardPool selectCardPoolById(Integer cardId)
    {
        return cardPoolMapper.selectCardPoolById(cardId);
    }

    /**
     * 卡池是否存在
     *
     * @param cardPool 卡池
     * @return 卡池
     */
    public CardPool checkCardPool(CardPool cardPool){
        return cardPoolMapper.checkCardPool(cardPool);
    }

    /**
     * 根据卡池所属scope、类型type查询卡池
     *
     * @param cardScope
     * @param cardType
     * @return 卡池
     */
    public CardPool selectCardPoolByScopeAndType(Integer cardScope, Integer cardType){
        return cardPoolMapper.selectCardPoolByScopeAndType(cardScope,cardType);
    }

    /**
     * 查询卡池列表
     * 
     * @param cardPool 卡池
     * @return 卡池
     */
    @Override
    public List<CardPool> selectCardPoolList(CardPool cardPool)
    {
        return cardPoolMapper.selectCardPoolList(cardPool);
    }

    /**
     * 新增卡池
     * 
     * @param cardPool 卡池
     * @return 结果
     */
    @Override
    public int insertCardPool(CardPool cardPool)
    {
        cardPool.setCreateTime(DateUtils.getNowDate());
        return cardPoolMapper.insertCardPool(cardPool);
    }

    /**
     * 修改卡池
     * 
     * @param cardPool 卡池
     * @return 结果
     */
    @Override
    public int updateCardPool(CardPool cardPool)
    {
        cardPool.setUpdateTime(DateUtils.getNowDate());
        return cardPoolMapper.updateCardPool(cardPool);
    }

    /**
     * 删除卡池对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCardPoolByIds(String ids)
    {
        return cardPoolMapper.deleteCardPoolByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除卡池信息
     * 
     * @param cardId 卡池ID
     * @return 结果
     */
    public int deleteCardPoolById(Integer cardId)
    {
        return cardPoolMapper.deleteCardPoolById(cardId);
    }

    /**
     * 检查卡池余量并发送告警邮件
     *
     * @return 结果
     */
    public void checkCardPoolAndSendEmail(){
        List<CardPool> cardPoolList = cardPoolMapper.selectCardPoolList(null);
        boolean send = false;
        if (null != cardPoolList && cardPoolList.size() > 0){
            String subject = "卡池余量不足告警";
            StringBuilder content = new StringBuilder("<html><head></head><body>");
            content.append("<h5>请注意：<br />\n以下卡池余量不足：\n</h5>");
            content.append("<table border=\"5\" style=\"border:solid 1px #E8F2F9;font-size=14px;;font-size:18px;\">");
            content.append("<tr style=\"background-color: #F0F0F0; color:#000000\"><th>卡池</th><th>类型</th><th>预估余量</th><th>实际余量</th><th>提醒余量</th>");
            content.append("</tr>");
            for (CardPool cp : cardPoolList){
                String cardScope = dictDataMapper.selectDictLabel("card_scope", cp.getCardScope() + "") ;
                String cardTypee = dictDataMapper.selectDictLabel("card_type", cp.getCardType() + "");
                if (cp.getPredictRemainder() <= cp.getWarnRemainder() || cp.getActualRemainder() <= cp.getWarnRemainder()){
                    send = true;
                    content.append("<tr>");
                    content.append("<td>" + cardScope + "</td>"); //第一列
                    content.append("<td>" + cardTypee + "</td>"); //第二列
                    content.append("<td>" + cp.getPredictRemainder() + "</td>"); //第三列
                    content.append("<td>" + cp.getActualRemainder() + "</td>"); //第四列
                    content.append("<td>" + cp.getWarnRemainder() + "</td>"); //第四列
                    content.append("</tr>");
                }
            }
            content.append("</table>");
            content.append("</body></html>");
            if (send){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("subject",subject);
                jsonObject.put("content",content);
                rabbitmqTemplate.convertAndSend(RabbitConstant.DIRECT_CARD_POOL_EMAIL_EXCHANGE,RabbitConstant.CARD_POOL_EMAIL_ROUTING,jsonObject.toString());
            }
        }
    }
}
